package com.xfsong.demo.service;

import com.xfsong.demo.entity.Goods;
import com.xfsong.demo.repository.GoodsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
public class GoodsService {
    @Autowired
    private GoodsRepository goodsRepository;
    //保存商品信息
    public Goods saveGoods(Goods goods) {
        return goodsRepository.save(goods);
    }

    //验证商品名称是否存在
    public boolean isGoodsNameExist(String name) {
        return goodsRepository.existsByName(name);
    }

    //分页商品信息
    public Page<Goods> getGoodsByPage(Pageable pageable) {
        return goodsRepository.findAll(pageable);
    }
    //分页商品信息并排序
    public Page<Goods> getGoodsByPageAndSort(Pageable pageable) {
        return goodsRepository.findAll(pageable);
    }
    //根据商品名称筛选并分页商品信息
    public Page<Goods> getGoodsByNameAndPage(String name, Pageable pageable) {
        return goodsRepository.findByNameContaining(name, pageable);
    }

    //根据商品id删除商品信息
    public void deleteGoodsById(Long id) {
        goodsRepository.deleteById(id);
    }

    //根据商品id获取商品信息
    public Goods getGoodsById(Long id) {
        return goodsRepository.findById(id).orElse(null);
    }

    //修改商品信息
    public Goods updateGoods(Goods goods) {
        return goodsRepository.save(goods);
    }


}
